package com.boot2.core.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhangweilin
 * @description: mqtt主题工具类
 * @date 2018/12/13
 */
public class MqttTopicUtil {
    /**
     * 提取当前配置的主题的前缀，如/yoju/+/up/#，取到 /yoju
     *
     * @param topic
     * @return
     */
    public static String getTopicPrefix(String topic) {
        String reg = (".*?[+#]");
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(topic);
        if (matcher.find()) {
            topic = matcher.group();
            topic = topic.replaceAll("[+#]", "");
        }
        return topic;
    }
}
